package com.yj.forum.services.impl;

import com.yj.forum.common.AppResult;
import com.yj.forum.common.ResultCode;
import com.yj.forum.dao.BoardMapper;
import com.yj.forum.exception.ApplicationException;
import com.yj.forum.model.Board;
import com.yj.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;
    /**
     * 查询前 num个正常状态的版块
     * @param num 查询的个数
     * @return 前 num个版块的集合
     */
    @Override
    public List<Board> selectByNum(Integer num) {
        //参数校验
        if(num <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao层
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //非空校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null) {
            log.info(ResultCode.ERROR_IS_NULL.toString() + ", board id = " + id);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        // 更新帖⼦数量
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        // 调⽤DAO，执⾏更新
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断行数
        if(row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的⾏数不等于 1 ,为 " + row);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        return boardMapper.selectByPrimaryKey(id);

    }
    /**
     * 版块中的帖⼦数量 -1
     * @param id 版块Id
     */
    @Override
    public void subOneArticleCountById(Long id) {
        //非空校验
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_BOARD_IS_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_IS_NOT_EXISTS));
        }
        //调用dao 拿板块
        Board board = boardMapper.selectByPrimaryKey(id);
        //校验
        if(board == null || board.getState() != 0) {
            log.warn(ResultCode.FAILED_BOARD_IS_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_IS_NOT_EXISTS));
        }
        if(board.getDeleteState() != 0) {
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_BANNED));
        }
        //构造更新对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount((board.getArticleCount() == null || board.getArticleCount() == 0) ? 0 : board.getArticleCount()-1);
        //调用dao，更新数据库
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //校验
        if(row != 1) {
            log.error(ResultCode.ERROR_SERVICES.toString()+ ", 受影响的⾏数不等于 1 为 " + row);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }
}
